<%--
    Document   : index
    Created on : Oct 14, 2012, 10:12:11 PM
    Author     : aspr0
--%>

<%@page import="javax.ejb.RemoveException"%>
<%@page import="is.web.htgen.multiple.EmployeeTable"%>
<%@page import="is.bean.employee.Employee"%>
<%@page import="is.bean.employee.EmployeeBeanRemote"%>
<%@page import="is.web.htgen.single.EmployeeForm"%>
<%@page import="is.bean.employee.EmployeeBeanRemoteHome"%>
<%@page import="is.bean.department.Department"%>
<%@page import="is.bean.department.DepartmentBeanRemote"%>
<%@page import="is.bean.department.DepartmentBeanRemoteHome"%>
<%@page import="is.bean.region.RegionBeanRemoteHome"%>
<%@page import="is.web.Checker"%>
<%@page import="is.web.Message"%>
<%@page import="is.web.htgen.multiple.DepartmentTable"%>
<%@page import="is.web.htgen.single.DepartmentForm"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@page import="javax.ejb.CreateException"%>
<%@page import="javax.ejb.FinderException"%>

<%
    //request.setCharacterEncoding("UTF-8");

    String rudHtml = "generation error";
    String createHtml = "generation error";
    String rudTitle = "View";
    String msgHtml = "";

    try
    {
        Context initial = new InitialContext();
        Object depRef = initial.lookup("ejb/DepartmentBean");
        Object empRef = initial.lookup("ejb/EmployeeBean");

        DepartmentBeanRemoteHome departmentHome =
                (DepartmentBeanRemoteHome) PortableRemoteObject.narrow(depRef, DepartmentBeanRemoteHome.class);
        EmployeeBeanRemoteHome employeeHome =
                (EmployeeBeanRemoteHome) PortableRemoteObject.narrow(empRef, EmployeeBeanRemoteHome.class);

        EmployeeForm form = new EmployeeForm();

        form.setDepartmentHome(departmentHome);
        createHtml = form.genCreate();

        if(Checker.isZeroPositiveInteger(request.getParameter("i")))
        {
            msgHtml = Message.info(request.getParameter("i"));
        }
        else if(Checker.isZeroPositiveInteger(request.getParameter("w")))
        {
            msgHtml = Message.warning(request.getParameter("w"));
        }
        else if(Checker.isZeroPositiveInteger(request.getParameter("e")))
        {
            msgHtml = Message.error(request.getParameter("e"));
        }

        if (Checker.isPositiveInteger(request.getParameter("id")) &&
                request.getParameter("act")!=null &&
                request.getParameter("create")==null )
        {
            Integer id = Integer.parseInt(request.getParameter("id"));
            EmployeeBeanRemote employee = employeeHome.findByPrimaryKey(id);
            form.setBean(employee);

            if ("update".equals(request.getParameter("act")))
            {
                rudHtml = form.genCopyUpdate(true);
                rudTitle = "Update";
%>

<jsp:useBean id="updateEmployee" class="is.bean.employee.Employee"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="updateEmployee" property="*"/>

<%
                if (request.getMethod().equals("POST"))
                {
                    employee.setData(updateEmployee);
                    response.sendRedirect("?i=0");
                }
            }
            else if ("copy".equals(request.getParameter("act")))
            {
                rudHtml = form.genCopyUpdate(false);
                rudTitle = "Copy";
%>

<jsp:useBean id="copyEmployee" class="is.bean.employee.Employee"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="copyEmployee" property="*"/>

<%
                if (request.getMethod().equals("POST"))
                {
                    employeeHome.createInstance(copyEmployee);
                    response.sendRedirect("?i=4");
                }
            }
            else if ("delete".equals(request.getParameter("act")))
            {
                rudHtml = form.genDelete();
                rudTitle = "Delete";

                if (request.getMethod().equals("POST"))
                {
                    employeeHome.remove(id);
                    response.sendRedirect("?i=1");
                }
            }
            else
            {
                rudHtml = form.genRetrieve();
            }
        }
        else if("create".equals(request.getParameter("create")) && request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="createEmployee" class="is.bean.employee.Employee"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="createEmployee" property="*"/>

<%
            employeeHome.createInstance(createEmployee);
            response.sendRedirect("?i=2");
        }
        else
        {
            Employee employeeExample = new Employee();
            EmployeeTable table = new EmployeeTable();
            table.setDepartmentHome(departmentHome);
            Boolean searchPerformed = false;

            if (request.getMethod().equals("POST"))
            {
                try
                {
%>

<jsp:useBean id="findEmployee" class="is.bean.employee.Employee"/> <%--FULL PATH!!!--%>
<jsp:setProperty name="findEmployee" property="*"/>

<%
                    employeeExample = findEmployee;
                    searchPerformed = true;
                }
                catch(Exception e) /* Lazy catching nested number format exception */
                {
                    msgHtml = Message.warning("0");
                }
            }

            Collection<EmployeeBeanRemote> employees = employeeHome.findByExample(employeeExample);
            table.setEmployees(employees);
            table.setFindExample(employeeExample);

            if(searchPerformed)
            {
                if(employees.size()<1)
                {
                    msgHtml = Message.warning("3");
                }
                else
                {
                    msgHtml = Message.info("3");
                }
            }

            rudHtml = table.generate();
        }
    }
    catch(RemoveException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=2");
    }
    catch(CreateException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=1");
    }
    catch(FinderException e)
    {
        //e.printStackTrace();
        response.sendRedirect("?w=5");
    }
    catch(Exception e)
    {
        //e.printStackTrace();
        response.sendRedirect("?e=1");
    }
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=is.web.Locale.TITLE%></title>
        <link rel="stylesheet" href="../style/common.css">
        <link rel="stylesheet" href="../style/rud-table.css">
        <link rel="stylesheet" href="../style/msg.css">
        <script src="../script.js" type="text/javascript"></script>
    </head>
    <body>
        <h1><a href=".."><img src="../img/go-home_32.png" class="linkIcon"></a>| Employees</h1>

        <%=msgHtml%>

        <div class="spoiler" onclick="switchDisplay('frameCreate')">Create new one</div>
        <div class="spoiled" id="frameCreate" style="display: none;">
            <%=createHtml%>
        </div>

        <div class="spoiler" onclick="switchDisplay('rudHtml')"><%=rudTitle%></div>
        <div class="spoiled" id="rudHtml">
            <%=rudHtml%>
        </div>


    </body>
</html>
